﻿using GenericHtml;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WLFramework.Json;
using WLFramework.Web;
using WLFramework.Data;
using WLFramework.Data.Linq;

namespace SampleSite.admin.ajax.table
{
    /// <summary>
    /// publish 的摘要说明
    /// </summary>
    public class publish : AjaxHttpHandler
    {
        public override void OnProcessRequest(HttpContext context)
        {
            int id = context.GetValueByName<int>("id");

            using (CommandHelper db = new CommandHelper(CommandMethod.Write))
            {
                db.Context.BeginTransaction();

                var table = (from p in db.Context.dy_DynamicTables
                             where p.State == 0 && p.Id == id
                             select new DynamicTable
                             {
                                 Id = p.Id,
                                 Name = p.TableName,
                                 DisplayName = p.Description
                             }).FirstOrDefault();

                if (table == null)
                    throw new ArgumentException("参数错误");

                db.Context.dy_DynamicTables
                  .Where(k => k.Id == table.Id)
                  .Set(k => k.State, 1)
                  .Update();

                // 读取数据
                var columns = (from p in db.Context.dy_DynamicColumns
                               where p.TableId == table.Id
                               select new DynamicColumn
                               {
                                   Name = p.ColumnName,
                                   DisplayName = p.DisplayName,
                                   TypeId = (ColumnType)p.TypeId,
                                   ControlId = p.ControlId,
                                   Precision = p.Precision,
                                   MinLength = p.MinLength,
                                   MaxLength = p.MaxLength,
                               }).ToList();

                // 创建SQL
                TableBuilder builder = new TableBuilder(table);
                foreach (var item in columns)
                {
                    builder.AddColumn(item);
                }

                // 执行SQL
                db.Context.SetCommand(builder.GetSql())
                  .ExecuteNonQuery();

                // 添加系统默认字段
                db.Context.dy_DynamicColumns
                  .Insert(() => new dy_DynamicColumns
                  {
                      TableId = table.Id,
                      ControlId = 0,
                      TypeId = (int)ColumnType.ID,
                      Precision = 0,
                      Required = true,
                      CanEdit = false,
                      IsSystemDefault = true,
                      ColumnName = "Id",
                      DisplayName = "ID",
                  });

                db.Context.dy_DynamicColumns
                  .Insert(() => new dy_DynamicColumns
                  {
                      TableId = table.Id,
                      ControlId = 0,
                      TypeId = (int)ColumnType.ID,
                      Required = true,
                      CanEdit = false,
                      Precision = 0,
                      IsSystemDefault = true,
                      ColumnName = "CreateUser",
                      DisplayName = "创建人",
                  });

                db.Context.dy_DynamicColumns
                  .Insert(() => new dy_DynamicColumns
                  {
                      TableId = table.Id,
                      ControlId = 0,
                      TypeId = (int)ColumnType.ID,
                      Required = true,
                      CanEdit = false,
                      IsSystemDefault = true,
                      Precision = 0,
                      ColumnName = "UpdateUser",
                      DisplayName = "修改人",
                  });

                db.Context.dy_DynamicColumns
                  .Insert(() => new dy_DynamicColumns
                  {
                      TableId = table.Id,
                      ControlId = 0,
                      TypeId = (int)ColumnType.DATETIME,
                      Required = true,
                      CanEdit = false,
                      Precision = 0,
                      IsSystemDefault = true,
                      ColumnName = "CreationTime",
                      DisplayName = "创建时间",
                  });

                db.Context.dy_DynamicColumns
                  .Insert(() => new dy_DynamicColumns
                  {
                      TableId = table.Id,
                      ControlId = 0,
                      TypeId = (int)ColumnType.DATETIME,
                      Required = true,
                      CanEdit = false,
                      IsSystemDefault = true,
                      Precision = 0,
                      ColumnName = "UpdatedTime",
                      DisplayName = "修改时间",
                  });

                // 重新组织数据
                // 添加到缓存管理
                MetadataManager.AddTable(Common.DynamicMetadataProvider.GetTable(db.Context, table.Name));

                db.Context.CommitTransaction();

                context.Response.Write(JSON.Instance.ToJSON(JSONResult.Success));
            }
        }
    }
}